import sys

from controllers.Custom_tableinfo import TableInfoManager
from controllers.Custom_treeview import TreeViewManager, DataTreeViewManager
from controllers.mouse_enent import CustomGraphicsScene
from models.node_model import *
# from ui.py_ui.UiMindMap import Ui_Form
from config.config import Config
from ui.py_ui.UiMindMap02 import Ui_Form

import ctypes

from views.set_css import set_css


class MainWindow(QWidget, Ui_Form):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)

        self.scene = CustomGraphicsScene(self)
        self.graphicsView.setScene(self.scene)
        self.context_menu_pos = QPointF(0, 0)
        self.tree_view_manager = TreeViewManager(self)
        self.data_tree_view_manager = DataTreeViewManager(self)
        self.table_info_manager = TableInfoManager(self)
        self.setWindowTitle("导弹WQ系统生存能力分析系统")
        icon_path = os.path.join(Config.svg_path, "main_icon.png")
        icon = QtGui.QIcon(icon_path)
        self.setWindowIcon(icon)

        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)  # 无边框
        self.is_maximized = False
        self.toolButton_4.clicked.connect(self.toggle_fullscreen)

        self.widget_4.setStyleSheet("image: url(:/qrc/svg/main_icon.svg);")
        ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("6666")

        self.drag_position = QPoint()
        self.resize_edge_threshold = 10  # 边缘检测阈值
        self.resizing = False
        self.resize_direction = None
        set_css(self.graphicsView, os.path.join(Config.static_path, 'scroll_bar.css'))
        # self.graphicsView.setFrameStyle(0)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.drag_position = event.globalPos() - self.frameGeometry().topLeft()
            self.resizing = self.is_near_edge(event.pos())
            if self.resizing:
                self.resize_direction = self.get_resize_direction(event.pos())
            event.accept()

    def mouseMoveEvent(self, event):
        if event.buttons() == Qt.LeftButton:
            if self.resizing:
                self.resize_window(event.globalPos())
            else:
                self.move(event.globalPos() - self.drag_position)
            event.accept()

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.resizing = False
            self.resize_direction = None
            event.accept()

    def update_cursor_shape(self, pos):
        if self.is_near_edge(pos):
            direction = self.get_resize_direction(pos)
            if direction == "top-left" or direction == "bottom-right":
                self.setCursor(QCursor(Qt.SizeFDiagCursor))
            elif direction == "top-right" or direction == "bottom-left":
                self.setCursor(QCursor(Qt.SizeBDiagCursor))
            elif direction == "left" or direction == "right":
                self.setCursor(QCursor(Qt.SizeHorCursor))
            elif direction == "top" or direction == "bottom":
                self.setCursor(QCursor(Qt.SizeVerCursor))
        else:
            self.setCursor(QCursor(Qt.ArrowCursor))

    def is_near_edge(self, pos):
        return (
                pos.x() < self.resize_edge_threshold or
                pos.y() < self.resize_edge_threshold or
                self.width() - pos.x() < self.resize_edge_threshold or
                self.height() - pos.y() < self.resize_edge_threshold
        )

    def get_resize_direction(self, pos):
        if pos.x() < self.resize_edge_threshold:
            if pos.y() < self.resize_edge_threshold:
                return "top-left"
            elif self.height() - pos.y() < self.resize_edge_threshold:
                return "bottom-left"
            else:
                return "left"
        elif self.width() - pos.x() < self.resize_edge_threshold:
            if pos.y() < self.resize_edge_threshold:
                return "top-right"
            elif self.height() - pos.y() < self.resize_edge_threshold:
                return "bottom-right"
            else:
                return "right"
        elif pos.y() < self.resize_edge_threshold:
            return "top"
        elif self.height() - pos.y() < self.resize_edge_threshold:
            return "bottom"
        return None

    def resize_window(self, global_pos):
        delta = global_pos - self.frameGeometry().topLeft()
        new_geometry = self.frameGeometry()

        if "left" in self.resize_direction:
            new_geometry.setLeft(global_pos.x())
        if "right" in self.resize_direction:
            new_geometry.setRight(global_pos.x())
        if "top" in self.resize_direction:
            new_geometry.setTop(global_pos.y())
        if "bottom" in self.resize_direction:
            new_geometry.setBottom(global_pos.y())

        self.setGeometry(new_geometry)

    # 切换全屏
    def toggle_fullscreen(self):
        if self.is_maximized:
            self.showNormal()
            full_icon = QtGui.QIcon()
            full_icon.addPixmap(QtGui.QPixmap(os.path.join(Config.svg_path, 'full_screen.svg')), QtGui.QIcon.Normal,
                                QtGui.QIcon.Off)
            self.toolButton_4.setIcon(full_icon)
            self.is_maximized = False
        else:
            self.showMaximized()
            full_icon = QtGui.QIcon()
            full_icon.addPixmap(QtGui.QPixmap(os.path.join(Config.svg_path, 'min_screen.svg')), QtGui.QIcon.Normal,
                                QtGui.QIcon.Off)
            self.toolButton_4.setIcon(full_icon)
            self.is_maximized = True


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())
